//
//  RBSearchNaviBar.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/24.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "RBSearchNaviBar.h"
//搜索框颜色
//#define kSearchBarTFDColor UIColorHex(0x00B38A)
////文字颜色
//#define kMainTextColor UIColorHex(0x646464)
//
//#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface RBSearchNaviBar()<UISearchBarDelegate>
@property(nonatomic, strong) UIButton *cancelButton; // 取消按钮

@end
@implementation RBSearchNaviBar

#pragma mark - 一 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        self.backgroundColor = kSearchBarTFDColor;
        [self setupView];
    }
    return self;
}
- (void)setupView {
 
    [self addSubview:self.searchBar];
    
    [self addSubview:self.cancelButton];
}

#pragma mark - 二 lazy
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        _cancelButton.backgroundColor = [UIColor redColor];
        [_cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        
        [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelButton;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        // 创建对象
        _searchBar = [[UISearchBar alloc] init];
        
        // 设置属性
        _searchBar.delegate =self;
        _searchBar.placeholder = @"请输入帖子标题";
        _searchBar.layer.cornerRadius = 15;
        _searchBar.layer.masksToBounds = YES;
        
        //UISearchBar 默认有一个灰色的view  使用backgroundImage 可以去掉
        _searchBar.backgroundImage = [self imageFromColor:[UIColor whiteColor]];
//        _searchBar.backgroundColor = [UIColor whiteColor]; // 没用
    
        // 放大镜icon
        UIImage *leftImage = [UIImage imageNamed:@"nav_search"];
        [_searchBar setImage:leftImage forSearchBarIcon:(UISearchBarIconSearch) state:(UIControlStateNormal)];
        
        // 清除按钮
        UIImage *clearImage = [UIImage imageNamed:@"close_black"]; // 叉叉
        [_searchBar setImage:clearImage forSearchBarIcon:(UISearchBarIconClear) state:(UIControlStateNormal)];
        
        // 固定写法:修改searchBar输入框的样式
        UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
        searchField.enablesReturnKeyAutomatically = YES;
        [searchField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [searchField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
        //        searchField.backgroundColor = kSearchBarTFDColor; //qi
        searchField.backgroundColor = XJCOLOR_RGB(240, 240, 240);
        searchField.textColor = [UIColor lightGrayColor];
    }
    return _searchBar;
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat searchW = KWidth - 80;
    CGFloat kh = 40;
    
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.bottom.equalTo(self);
        make.left.equalTo(self);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(searchW, kh));
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchBar.mas_right).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(kh);
        make.centerY.equalTo(self.mas_centerY); // 水平居中
    }];
}
#pragma mark - 三 事件回调
- (void)cancelButtonAction{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}
#pragma mark - 四 UISearchBarDelegate
//- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//
//}

//点击清除按钮 || 呼出键盘
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    return YES;
}

// 点击搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    [searchBar resignFirstResponder]; // 回收键盘
    if (self.searchBlock) {
        self.searchBlock(searchBar.text);
    }
}


#pragma mark - 私有方法

//覆盖默认占位文字
- (void)setPlaceholder:(NSString *)placeholder {
    if ([NSString isEmptyString:placeholder]) return;
    
    _placeholder = placeholder;
    self.searchBar.placeholder = placeholder;
}

// 将color 转为image
- (UIImage *)imageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    // 获取当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置颜色
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    // 填充矩形区域
    CGContextFillRect(context, rect);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}
@end
